<?php 
/**
 * @version        $Id: route.php 21097 2011-04-07 15:38:03Z dextercowley 
 * @package        Joomla.Site
 * @subpackage    com_content
 * @copyright    Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
 * @license        GNU General Public License version 2 or later; see LICENSE.txt
 */

/*no direct access */
defined('_JEXEC') or die;

jimport('joomla.application.component.helper');
jimport('joomla.application.categories');

/**
 * Content Component Route Helper
 * @static
 * @package        Joomla.Site
 * @subpackage    com_content
 * @since 1.5
 */
abstract class WrdHelperRoute
{
    protected static $lookup;
    
    public static function getProfileRoute($id = 0)
    {
		if($id == 0){
            $needles = array(
                'profile'      => array(),
            );
            $link = 'index.php?option=com_wrd&view=profile&layout=default';
        }
        else{
            $needles = array(
                'profile'      => array(),
                'id'        => array($id)
            );
            $link = 'index.php?option=com_wrd&view=profile&id='.$id;
        }
        
        if ($item = WrdHelperRoute::_findItem($needles)) {
            $link .= '&Itemid='.$item->id;
        }
        return $link;
    }
    
    public static function getProfileResetRoute()
    {
        $needles = array(
            'forgot'    => array(),
            'profile'      => array()
        );
        
        $link = 'index.php?option=com_wrd&view=profile&layout=reset';
        
        if ($item = WrdHelperRoute::_findItem($needles)) {
            $link .= '&Itemid='.$item->id;
        }
        return $link;
    }
    
    public static function getProfileLoginRoute()
    {
        $needles = array(
            'login'    => array(),
            'profile'      => array()
        );
        
        $link = 'index.php#login';
        return $link;
    }
    
    public static function getProfileFinishRoute()
    {
        $needles = array(
            'finish'    => array(),
            'profile'      => array()
        );
        
        $link = 'index.php?option=com_wrd&view=profile&layout=finish';
        
        if ($item = WrdHelperRoute::_findItem($needles)) {
            $link .= '&Itemid='.$item->id;
        }
        return $link;
    }
    
    public static function getMyProfileRoute()
    {
    	$needles = array(
    			'finish'    => array(),
    			'profile'      => array()
    	);
    
    	$link = 'index.php?option=com_wrd&view=profile&layout=myaccount';
    	
    	if ($item = WrdHelperRoute::_findItem($needles)) {
    		$link .= '&Itemid='.$item->id;
    	}
    	return $link;
    }
    
    function _findItem($needles){

        $component = & JComponentHelper::getComponent('com_wrd');
        $menus = & JApplication::getMenu('site', array ());
        $items = $menus->getItems('component_id', $component->id);
        
        $match = null;
        foreach ($needles as $needle=>$id) {
            if (count($items)){
                foreach ($items as $item) {
                    if ((@$item->query['layout'] == $needle)) {

                        $match = $item;
                        break;
                    }
                    
                    if ((@$item->query['view'] == $needle)) {

                        $match = $item;
                        break;
                    }
                    
                    if (!is_null($match)) {
                        break;
                    }
                }
            }
            if (!is_null($match)) {
                break;
            }
        }
        return $match;
    }
    
}